package com.stu_ques.question.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.stu_ques.common.util.DateUtil;
import com.stu_ques.question.dao.IAnswerDao;
import com.stu_ques.question.dao.IQuestionDao;
import com.stu_ques.question.pojo.Answer;
import com.stu_ques.question.pojo.Question;
import com.stu_ques.question.service.IQuestionService;
import com.stu_ques.users.pojo.Student;
import com.stu_ques.users.service.IUsersService;

public class QuestionServiceImpl implements IQuestionService
{
	private IQuestionDao questionDao;
	
	private IAnswerDao answerDao;
	private IUsersService usersService;
	private DateUtil dateUtil = new DateUtil();
	@Override
	/**
	 * 	在Service中 增加一个问题	
	 * */
	public int saveOneQuestionService(Question question)
	{
		return this.questionDao.saveOneDao(question);
	}
	/**
	 * 	在Service中 更新一个问题	
	 * */
	@Override
	public void updateOneQuestionService(Question question)
	{
		this.questionDao.updateOneDao(question);
	}
	/**
	 * 	在Service中 删除一个问题	
	 * */
	@Override
	public void deleteOneQuestionService(Question question)
	{
		this.questionDao.deleteOneDao(question);
	}
	/**
	 * 	在Service中 查找一个问题	
	 * */
	@Override
	public Question selectQuestionService(Map<String, Object> condMap)
	{
		return this.questionDao.selectOneDao(condMap);
	}
	/**
	 * 	在Service中	查找所有问题	
	 * */
	@Override
	public List<Question> selectListQuestionService()
	{
		return this.questionDao.selectListDao();
	}
	/**
	 * 	在Service中 添加问题的标题
	 * */
	@Override
	public String printTitleQuestionService()
	{
		return "id\t名字\t得分\t人数\t创建时间\t\t结束时间";
	}
	/**
	 * 	在Service中	添加一个答案方法	
	 * */
	@Override
	public int saveOneAnswerService(Answer answer)
	{
		int id = this.answerDao.saveOneDao(answer);
		/*===问题的POJO===*/
		/*	先根据问题的id查询问题的POJO	*/
		Map<String, Object> condMap = new HashMap<String, Object>();
		/*	把Map清零	*/
		condMap.clear();
		/*	创建问题表的关联关系 此id对应答案的ID	*/
		condMap.put("id", answer.getQuestionId());
		/*	调用问题表的查询一个方法	*/
		Question question = this.selectQuestionService(condMap);
		/*	要更新一些属性	*/
		/*	查询所有的答案,把此问题的答案列出来	*/
		int count = 0;
		/*	查询所有答案	*/
		List<Answer> answersList = this.selectListAnswerService();
		for (Iterator iterator = answersList.iterator(); iterator.hasNext();)
		{
			Answer answer2 = (Answer) iterator.next();
			/*	如果循环的答案的问题和目标答案的问题一致	*/
			if(answer.getQuestionId() == answer2.getQuestionId())
			{
				count ++;
			}
		}
		question.setTotalNumber(count);
		/*	调用跟新问题方法	*/
		this.updateOneQuestionService(question);
		/*	更新学生	*/
		/*	把Map清零	*/
		condMap.clear();
		condMap.put("id", answer.getStudentId());
		/*	调用查找单个学生的方法 进行查找更新	*/
		Student student = this.usersService.selectStudentService(condMap);
		/*	总次数+1	*/
		student.setTotal( student.getTotal()+1);
		/*	要把昨天的时间清零	*/
		Date updateTime = student.getUpdateTime();
		boolean isyesFlag = this.dateUtil.isyesDay(new Date(), updateTime);
		if(isyesFlag)
		{
			/*	将时间清零	*/
			student.setTodayTotal(0);
			student.setTodayCorrect(0);
		}
		student.setTodayTotal(student.getTodayTotal()+1);
		if("对".equalsIgnoreCase(answer.getRightAndWrong()))
		{
			/*	判断当对的时候 真确次数+1	*/
			student.setCorrect(student.getCorrect()+1);
			student.setTodayCorrect(student.getTodayCorrect()+1);
		}
		/*	调用方法赋值	*/
		this.usersService.updateOneStudentService(student);
		return id;	
	}
	/**
	 * 	在Service中	更新一个答案的方法	
	 * */
	@Override
	public void updateOneAnswerService(Answer answer)
	{
		this.answerDao.updateOneDao(answer);
	}
	/**
	 * 	在Service中	删除一个答案的方法	
	 * */
	@Override
	public void deleteOneAnswerService(Answer answer)
	{
		this.answerDao.deleteOneDao(answer);
	}
	/**
	 * 	在Service中	查询一个答案的方法	
	 * */
	@Override
	public Answer selectAnswerService(Map<String, Object> condMap)
	{
		return this.answerDao.selectOneDao(condMap);
	}
	/**
	 * 	在Service中	查询所有答案的方法	
	 * */
	public List<Answer> selectListAnswerService()
	{
		List<Answer> answersList = this.answerDao.selectListDao();
		Map<String, Object> condMap = new  HashMap<String, Object>();
		/*	循环为问题和学生赋值	*/
		for (Answer answerTemp : answersList)
		{
			/*	查询问题:根据questionId查询问题	*/
			condMap.clear();
			condMap.put("id", answerTemp.getQuestionId());
			/*	调用Service进行赋值	*/
			Question question = this.selectQuestionService(condMap);
			answerTemp.setQuestion(question);
			/*	查询学生:根据studentId查询学生	*/
			condMap.clear();
			condMap.put("id", answerTemp.getStudentId());
			/*	调用Service进行赋值	*/
			Student student = this.usersService.selectStudentService(condMap);
			answerTemp.setStudent(student);
		}
		return answersList;
	}

	@Override
	public String printTitleAnswerService()
	{
		return "id\t问题\t学生\t答案\t对错\t创建时间\t\t结束时间";
	}
	public void setQuestionDao(IQuestionDao questionDao)
	{
		this.questionDao = questionDao;
	}
	public void setAnswerDao(IAnswerDao answerDao)
	{
		this.answerDao = answerDao;
	}
	public void setUsersService(IUsersService usersService)
	{
		this.usersService = usersService;
	}

}
